package com.atguigu.day4.keyed

import org.apache.flink.streaming.api.TimeCharacteristic
import org.apache.flink.streaming.api.functions.KeyedProcessFunction
import org.apache.flink.streaming.api.scala._
import org.apache.flink.util.Collector

object keyed {
  def main(args: Array[String]): Unit = {

    val env = StreamExecutionEnvironment.getExecutionEnvironment

    //    设置时间语义为事件时间
    env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)

    env.setParallelism(1)
    //
    val stream1 = env.socketTextStream("192.168.35.102", 9999, '\n')
      .map(line=>{
        var arr =line.split(" ")
        (arr(0),arr(1).toLong * 1000L)
      })
      .assignAscendingTimestamps(_._2)

    val stream2 = env.socketTextStream("192.168.35.102", 9998, '\n')
      .map(line=>{
        var arr =line.split(" ")
        (arr(0),arr(1).toLong * 1000L)
      })
      .assignAscendingTimestamps(_._2)

    stream1
      .union(stream2)
      .keyBy(_._1)
      .process(new keyed)
      .print()

    env.execute()

  }
  class keyed extends KeyedProcessFunction[String,(String,Long),String]{
    override def processElement(value: (String, Long), ctx: KeyedProcessFunction[String, (String, Long), String]#Context, out: Collector[String]): Unit = {
      out.collect("当前水位线时"+ctx.timerService().currentWatermark())
    }
  }
}
